package com.springbooti18n.config;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;

/**
 * xufan
 * 2019/1/4
 * 请求、响应、拦截 来一次执行一次
 */
public class MyLocaleResolver implements LocaleResolver {

    private final List<Locale> supportedLocales = new ArrayList<Locale>(4);

    private Locale defaultLocale;


    /**
     * Configure supported locales to check against the requested locales
     * determined via {@link HttpServletRequest#getLocales()}. If this is not
     * configured then {@link HttpServletRequest#getLocale()} is used instead.
     * @param locales the supported locales
     * @since 4.3
     */
    public void setSupportedLocales(List<Locale> locales) {
        this.supportedLocales.clear();
        if (locales != null) {
            this.supportedLocales.addAll(locales);
        }
    }

    /**
     * Return the configured list of supported locales.
     * @since 4.3
     */
    public List<Locale> getSupportedLocales() {
        return this.supportedLocales;
    }

    /**
     * Configure a fixed default locale to fall back on if the request does not
     * have an "Accept-Language" header.
     * <p>By default this is not set in which case when there is "Accept-Language"
     * header, the default locale for the server is used as defined in
     * {@link HttpServletRequest#getLocale()}.
     * @param defaultLocale the default locale to use
     * @since 4.3
     */
    public void setDefaultLocale(Locale defaultLocale) {
        this.defaultLocale = defaultLocale;
    }

    /**
     * The configured default locale, if any.
     * @since 4.3
     */
    public Locale getDefaultLocale() {
        return this.defaultLocale;
    }


    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        Locale defaultLocale = getDefaultLocale();

        String localName = request.getHeader("lang");

        if (defaultLocale != null && localName == null) {
            return defaultLocale;
        }
        if(defaultLocale==null){
            defaultLocale = Locale.CHINA;
        }
        System.out.println("本地语言环境"+defaultLocale.getLanguage()+"");
        System.out.println("传输过来的语言环境"+localName+"");
        Locale requestLocale = null;
        if(!StringUtils.isEmpty(localName)){
            String[] split = localName.split("_");
            if(split.length>1){
                requestLocale = new Locale(split[0], split[1]);
            }else{
                requestLocale = new Locale(split[0]);
            }
        }

        if (isSupportedLocale(requestLocale)) {
            return requestLocale;
        }
        Locale supportedLocale = findSupportedLocale(request);
        if (supportedLocale != null) {
            return supportedLocale;
        }
        return (defaultLocale != null ? defaultLocale : requestLocale);
    }

    private boolean isSupportedLocale(Locale locale) {
        List<Locale> supportedLocales = getSupportedLocales();
        return (supportedLocales.isEmpty() || supportedLocales.contains(locale));
    }

    private Locale findSupportedLocale(HttpServletRequest request) {
        Enumeration<Locale> requestLocales = request.getLocales();
        while (requestLocales.hasMoreElements()) {
            Locale locale = requestLocales.nextElement();
            if (getSupportedLocales().contains(locale)) {
                return locale;
            }
        }
        return null;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
        throw new UnsupportedOperationException(
                "Cannot change HTTP accept header - use a different locale resolution strategy");
    }
}
